package com.huxin.filter;

import com.huxin.pojo.AdminInfo;
import com.huxin.pojo.UserInfo;
import com.huxin.service.Userservice;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebFilter("/*")
public class Filter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("拦截成功，并解决了跨域问题和响应编码操作");
//        HttpServletResponse httpServletResponse= (HttpServletResponse) servletResponse;
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse res= (HttpServletResponse) servletResponse;

        servletRequest.setCharacterEncoding("utf-8");
        //处理响应乱码
        servletResponse.setContentType("text/html;charset=utf-8");

//        httpServletResponse.setContentType("text/json;charset=utf-8");
//        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
//        // 允许指定的请求方法访问
//        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
//        // 允许自定义的请求头部
//        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");

        filterChain.doFilter(servletRequest,servletResponse);


    }

    @Override
    public void destroy() {

    }
}
